//
//  DTdkodJLCFRedPackageControllerALtAUS.m
//  Jlcf_ios
//
//  Created by 邓伟浩 on 2019/4/19.
//  Copyright © 2019 ski. All rights reserved.
//

#import "DTdkodJLCFRedPackageControllerALtAUS.h"
#import "XvmSEfJLCFListRequestdkZIyb.h"
#import "qyjQrYJLCFMineRedPacketCellwndHmU.h"

@interface DTdkodJLCFRedPackageControllerALtAUS ()

@property (nonatomic, strong) NSMutableArray *hbArr;
@property (nonatomic, assign) int pagenumber;

@end

@implementation DTdkodJLCFRedPackageControllerALtAUS

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.title = @"优惠券";
    self.hbArr = [[NSMutableArray alloc] init];
    
    self.view.backgroundColor = QTZ_Gray237;
    
    [self loadData];
}

- (void)loadData {
    
    [self.hbArr removeAllObjects];
    _pagenumber = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:self.CUWORhdetailModelIRREMd.data.id forKey:@"borrowId"];
    [params setSafeObject:@"10000000" forKey:@"buyAmt"];
    [params setSafeObject:@"hblist" forKey:@"type"];
    [params setSafeObject:@(_pagenumber) forKey:@"page"];
    
    if([JLCFRegisterService shareService].hasToken){
        [XvmSEfJLCFListRequestdkZIyb welfareWith:params loadingInView:self.view success:^(qBSjUSJLCFListWelfareDataModelmtizAG *welfare) {
            
            if (welfare.hblist.count > 0) {
                [self.hbArr addObjectsFromSafeArray:welfare.hblist];
            } else {
                [self showNoMoreDataNotice:@"没有更多了"];
            }
            [self.tableView reloadData];
            [self endRefresh];
        } failure:^(NSString *error) {
            [self endRefresh];
            [self showNotice:error];
        }];
    }
    
}

- (void)loadMoreData {
    _pagenumber ++;
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:self.CUWORhdetailModelIRREMd.data.id forKey:@"borrowId"];
    [params setSafeObject:@"10000000" forKey:@"buyAmt"];
    [params setSafeObject:@"hblist" forKey:@"type"];
    [params setSafeObject:@(_pagenumber) forKey:@"page"];
    
    [XvmSEfJLCFListRequestdkZIyb welfareWith:params loadingInView:self.view success:^(qBSjUSJLCFListWelfareDataModelmtizAG *welfare) {
        [self hideNoMoreDataNotice];
        if (welfare.hblist.count > 0) {
            [self.hbArr addObjectsFromSafeArray:welfare.hblist];
        } else {
            self.tableView.mj_footer.hidden = YES;
        }
        [self.tableView reloadData];
        [self endLoadMore];
    } failure:^(NSString *error) {
        [self endLoadMore];
        [self showNotice:error];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.hbArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *rRHNlxheaderViewfJQeRl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 15)];
    rRHNlxheaderViewfJQeRl.backgroundColor = [UIColor clearColor];
    return rRHNlxheaderViewfJQeRl;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    qyjQrYJLCFMineRedPacketCellwndHmU *cell = [qyjQrYJLCFMineRedPacketCellwndHmU dequeueReusableCellForTableView:tableView];
    cell.welfare = [self.hbArr safeObjectAtIndex:indexPath.section];
    [cell reloadData];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FozWsFJLCFMineWelfareDataModelLetMnJ *VWtWqQdataModelcUgEkM = [self.hbArr safeObjectAtIndex:indexPath.section];
    
    if(self.amount.intValue < [VWtWqQdataModelcUgEkM.smoney integerValue]) {
        [self showNotice:@"购买金额低于红包限额"];
    } else {
        if (self.selectCompletion) {
            self.selectCompletion(VWtWqQdataModelcUgEkM);
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
    
}

@end
